package com.czl.base.data.bean.tengyun

import android.os.Parcelable
import android.text.TextUtils
import kotlinx.parcelize.Parcelize
import java.io.Serializable

/**
 * @author by xubin
 * @date on 2022/1/19.
 * PS: Not easy to write code please indicate.
 */
@Parcelize
class SortBean(
    var allStockNum: Int? = null,
    var applyId: Int? = null,
    var applyType: Int? = null,
    var borrowNum: Int? = null,
    var classificationName: String? = null,
    var companyId: Int? = null,
    var companyName: String? = null,
    var companyShortName: String? = null,
    var damageNum: Int? = null,
    var date: String? = null,
    var dateStr: String? = null,
    var deleStatus: Boolean? = null,
    var inventoryStatus: Int? = null,
    var isPasteCode: Boolean? = null,
    var isPasteCodeStr: String? = null,
    var lendNum: Int? = null,
    var lossesNum: Int? = null,
    var badNum: Int? = null,
    var damageId: Int? = null,
    var inventoryId: Int? = null,
    var lossesvarue: Int? = null,
    var manageMode: Int? = null,
    var manageModeStr: String? = null,
    var num: Int? = null,
    var pidPath: String? = null,
    var planId: Int? = null,
    var price: String? = "0.00",
    var propCompanyId: Int? = null,
    var propCompanyName: String? = "",
    var propCompanyShortName: String? = null,
    var receiveNum: Int? = null,
    var recordId: Int? = null,
    var returnNum: Int? = null,
    var showId: Int? = null,
    var showName: String? = null,
    var sortCode: String? = null,
    var sortId: Int? = null,
    var sortImg: String? = null,
    var sortModel: String? = "",
    var sortName: String? = "",
    var sortNum: Int? = 0,
    var sortPId: Int? = null,
    var sortType: Int? = null,
    var sortTypeStr: String? = null,
    var stockNum: Int? = null,
    var storehouseId: Int? = null,
    var storehouseLocatId: Int? = null,
    var storehouseLocatName: String? = null,
    var storehouseName: String? = null,
    var surplusNum: Int? = null,
    var unit: String? = null,
    var useMode: Int? = null,
    var useModeStr: String? = null,
    var useNum: Int? = null,
    var userCompanyId: Int? = null,
    var userCompanyName: String? = null,
    var userCompanyShortName: String? = null,
    var userDepartmentId: Int? = null,
    var userId: Int? = null,
    var userName: String? = null,
    var varuation: Int? = null,
    var varuationStr: String? = null,
    var warnNum: Int? = null,
    var classStatus: Int? = null,
    var applyNum: Int? = null,
    var itemViewType: Int = 0,
    var changeReceiveNum: Int? = null,
    var firstApplyNum: String? = "",
    var totalPrice: String? = "0.00",
    var isChangeSort: Boolean? = null,
    var excLocatId: Int? = null,
    var excLossesNum: Int? = null,
    var excNum: Int? = null,
    var excLocatName: String? = null,
    var damageComment: String? = null,
    var needReturnNum: Int? = null,
    var defaultNumType: Int? = null,
    var totalStockNum: Int? = null,
    var clickable: Boolean? = null,
    var valuation: Int? = null,
    var valuationStr: String? = null,
    var existCompany: Boolean? = null,
    var inSortId: Int? = null,
    var pIdPath: String? = null,
    var editable: Boolean? = null,
    var addSortType: Int? = null,
    var selected: Boolean? = false,
    var uuidSortString: String? = null,
    var manufactureDate: String? = null,
    var manufacturer: String? = null,
    var storageNum: Int? = null,
    var locatTotalNum: Int? = null,
    var unInStoreNum: Int? = null,
    var facilitySum: Int? = null,
    var model: String? = null,
    var facilityName: String? = null,
    var defaultStockNum: Int? = null,
    var applicationUseFaclityId: Int? = null,
    var firstFacilityCount: Int? = null,
    var inventoryNum: Int? = null,
    var inventoryResultStr: String? = null,
    var position: Int? = null,
    var inventorySortId: Int? = null,
    var inventoryStoreId: Int? = null,
    var applySotId: Int? = null,
    var moveSortId: Int? = null,
    var moveNum: Int? = null,
    var applySortId: Int? = null,
    var assetType: Int? = null,
    var moveStorehouseLocatId: Int? = null,
    var moveStorehouseLocatName: String? = null,
    var organizationId: String? = null,
    var isDel: Boolean = false,
    var productBean: ProductBean? = null,
    var parentRecordBean: RecordRemandBean? = null,
    var excProductList: List<ProductBean?>? = null,
    var locatList: MutableList<LocatListBean?>? = null,
    var productList: List<ProductBean>? = null,
    var recordList: List<RecordRemandBean>? = null,
    var changeSortList: List<SortBean>? = null,
    var receiveList: MutableList<ReceiveBean>? = null,
    var borrowList: MutableList<BorrowBean>? = null,
) : BaseNumBean(), Parcelable {
    override val itemType: Int
        get() = if (itemViewType != 0) {
            itemViewType
        } else {
            SortBeanType.Type.VIEW_TYPE_SORT
        }

    fun clickable(): Boolean {
        return if (clickable == null) {
            true
        } else {
            clickable!!
        }
    }

    fun selected(): Boolean {
        return if (selected == null) {
            false
        } else {
            selected!!
        }
    }

    fun editable(): Boolean {
        return if (editable == null) {
            false
        } else {
            editable!!
        }
    }

    fun manageMode(): Int {
        return if (manageMode == null) {
            0
        } else {
            manageMode!!
        }
    }

    fun pasteCode(): Boolean {
        return if (isPasteCode == null) {
            false
        } else {
            isPasteCode!!
        }
    }

    fun needReturnNum(): Int {
        return if (needReturnNum == null) {
            0
        } else {
            needReturnNum!!
        }
    }

    fun excLossesNum(): Int {
        return if (excLossesNum == null) {
            0
        } else {
            excLossesNum!!
        }
    }

    fun excNum(): Int {
        return if (excNum == null) {
            0
        } else {
            excNum!!
        }
    }

    fun stockNum(): Int {
        return if (stockNum == null) {
            0
        } else {
            stockNum!!
        }
    }

    fun applyNum(): Int {
        return if (applyNum == null) {
            0
        } else {
            applyNum!!
        }
    }

    fun lendNum(): Int {
        return if (lendNum == null) {
            0
        } else {
            lendNum!!
        }
    }

    fun returnNum(): Int {
        return if (returnNum == null) {
            0
        } else {
            returnNum!!
        }
    }

    fun damageNum(): Int {
        return if (damageNum == null) {
            0
        } else {
            damageNum!!
        }
    }

    fun intactNum(): Int {
        return returnNum() - damageNum()
    }

    fun getCanNum(): Int {
        return applyNum() - lendNum() - getReceiveCount()
    }

    fun actionableNum(): Int {
        if (applyNum == null) {
            return 0
        }
        return if (lendNum == null) {
            applyNum!!
        } else applyNum!! - lendNum!!
    }

    fun getLocationProductNum(): Int {
        if (locatList == null || locatList!!.isEmpty()) {
            return 0
        }
        var total = 0
        for (bean in locatList!!) {
            if (bean == null) {
                continue
            }
            val productList = bean.productList
            if (productList != null) {
                total += productList.size
            }
        }
        return total
    }

    fun getExistCompanyBol(isInCompany: Boolean): Boolean {
        if (isInCompany) {
            return true
        }
        return if (inSortId != sortId) {
            true
        } else existCompany != null && existCompany!!
    }

    fun getAllocatedInNum(): Int {
        if (lendNum == null) {
            return 0
        }
        return if (returnNum == null) {
            lendNum!!
        } else lendNum!! - returnNum!!
    }

    fun getAllocateInNum(): Int {
        if (locatList == null || locatList!!.isEmpty()) return 0
        var total = 0
        for (bean in locatList!!) {
            if (bean == null) {
                continue
            }
            val productList = bean.productList
            total += if (productList != null && productList.isNotEmpty()) {
                productList.size
            } else {
                bean.stockNumInt
            }
        }
        return total
    }

    fun getAllocateCount(): Int {
        var total = 0
        if (locatList != null && locatList!!.size > 0) {
            for (listBean in locatList!!) {
                if (listBean == null) {
                    continue
                }
                val stockNum = listBean.stockNum
                total += stockNum ?: 0
            }
        } else {
            if (productList != null) {
                total = productList!!.size
            }
        }
        return total
    }

    fun getClassIds(isSortType: Boolean?): List<Int> {
        val list: MutableList<Int> = ArrayList()
        if (isSortType != null) {
            if (isSortType) {
                list.add(1)
            } else {
                list.add(2)
            }
        } else {
            list.add(1)
            list.add(2)
        }
        return list
    }

    fun getAddWarehouseNum(): Int {
        if (locatList == null || locatList!!.isEmpty()) return 0
        var total = 0
        for (bean in locatList!!) {
            bean?.storageNum.let {
                if (it != null) {
                    total += it
                }
            }
        }
        return total
    }

    fun getTotalReturnNum(): Int {
        if (recordList == null || recordList!!.isEmpty()) return 0
        var total = 0
        for (bean in recordList!!) {
            val returnNum = bean.returnNum
            if (returnNum != null) {
                total += returnNum
            }
        }
        return total
    }

    fun getTotalStorageNum(storehouseLocatId: Int): Int {
        if (locatList == null) return 0
        var totalCount = 0
        for (locatListBean in locatList!!) {
            if (locatListBean == null) {
                continue
            }
            val storageNum = locatListBean.storageNum
            totalCount += storageNum
            if (locatListBean.storehouseLocatId == storehouseLocatId) {
                defaultStockNum = if (stockNum == null) 0 else stockNum
            }
        }
        return totalCount
    }

    fun getTotalRecordDamageNum(): Int {
        if (recordList == null || recordList!!.isEmpty()) {
            return 0
        }
        var totalNum = 0
        for (recordRemandBean in recordList!!) {
            totalNum += recordRemandBean.excNumInt
        }
        return totalNum
    }

    fun setDefaultLocat(
        num: Int,
        storehouseLocatId: Int,
        storehouseLocatName: String?,
        numType: Int
    ) {
        defaultNumType = numType
        var locationList: MutableList<LocatListBean?>? = locatList
        var defaultLocat: LocatListBean? = null
        if (locationList == null) {
            locationList = ArrayList()
        } else {
            //是否包含临时仓位
            for (listBean in locationList) {
                if (listBean == null) {
                    continue
                }
                if (listBean.storehouseLocatId == storehouseLocatId) {
                    defaultLocat = listBean
                    break
                }
            }
        }
        if (defaultLocat == null) {
            defaultLocat = LocatListBean()
            defaultLocat.defaultNumType = LocatListBean.NUM_TYPE_STORAGE
            defaultLocat.storehouseLocatId = storehouseLocatId
            defaultLocat.storehouseLocatName = storehouseLocatName
            locationList.add(0, defaultLocat)
        }
        if (totalStockNum == null) {
            defaultLocat.storageNum = num
        } else {
            defaultLocat.storageNum = num - totalStockNum!!
        }
        this.locatList = locationList
    }

    private fun setOrderDefaultLocat(storehouseLocatId: Int, numType: Int) {
        defaultNumType = numType
        var defaultLocat: LocatListBean? = null
        var locationList: MutableList<LocatListBean?>? = this.locatList
        if (locationList == null) {
            locationList = ArrayList()
        } else {
            //是否包含临时仓位
            for (listBean in locationList) {
                if (listBean == null) {
                    continue
                }
                if (listBean.storehouseLocatId == storehouseLocatId) {
                    defaultLocat = listBean
                    break
                }
            }
        }
        if (defaultLocat == null) {
            defaultLocat = LocatListBean()
            defaultLocat.defaultNumType = LocatListBean.NUM_TYPE_STORAGE
            defaultLocat.storehouseLocatId = storehouseLocatId
            defaultLocat.storehouseLocatName = "默认库位"
            locationList.add(0, defaultLocat)
        }
        //        defaultLocat.setStorageNum(num - totalStockNum);
        this.locatList = locationList
    }

    fun getLocText(
        isOrder: Boolean,
        storehouseLocatId: Int,
        storehouseLocatName: String,
        numType: Int
    ): String {
        if (locatList == null || locatList!!.size == 0) {
            if (isOrder) {
                setOrderDefaultLocat(storehouseLocatId, numType)
            }
            return storehouseLocatName
        }
        val text = java.lang.StringBuilder()
        totalStockNum = 0
        if (numType == defaultNumType) {
            for (locatListBean in locatList!!) {
                if (locatListBean == null) {
                    continue
                }
                if (locatListBean.stockNumInt == 0) {
                    continue
                }
                if (storehouseLocatId != locatListBean.storehouseLocatId) {
                    totalStockNum = totalStockNum!! + locatListBean.stockNum
                }
                text.append(locatListBean.storehouseLocatName)
                    .append("(")
                    .append(locatListBean.stockNum)
                    .append(") ")
            }
        } else {
            var totalNum = 0
            for (locatListBean in locatList!!) {
                if (locatListBean == null) {
                    continue
                }
                totalNum += locatListBean.storageNum
                if (locatListBean.storageNum == 0) {
                    continue
                }
                text.append(locatListBean.storehouseLocatName)
                    .append("(")
                    .append(locatListBean.storageNum)
                    .append(") ")
            }
            if (totalNum <= 0) {
                return storehouseLocatName
            } else {
                if (!isOrder) {
                    this.storageNum = totalNum
                }
            }
        }
        if (text.isEmpty()) {
            return storehouseLocatName
        }
        return text.toString()
    }

    fun setDefaultLocatStock(num: Int, storehouseLocatId: Int, storehouseLocatName: String) {
        defaultNumType = SortNumType.Type.NUM_TYPE_STOCK
        var locationList: MutableList<LocatListBean?>? = this.locatList
        var defaultLocat: LocatListBean? = null
        if (locationList == null) {
            locationList = ArrayList()
        } else {
            //是否包含临时仓位
            for (bean in locationList) {
                if (bean == null) {
                    continue
                }
                if (bean.storehouseLocatId == storehouseLocatId) {
                    defaultLocat = bean
                    break
                }
            }
        }
        if (defaultLocat == null) {
            defaultLocat = LocatListBean()
            defaultLocat.defaultNumType = LocatListBean.NUM_TYPE_STOCK
            defaultLocat.storehouseLocatId = storehouseLocatId
            defaultLocat.storehouseLocatName = storehouseLocatName
            locationList.add(0, defaultLocat)
        }
        if (totalStockNum == null) {
            defaultLocat.stockNum = num
        } else {
            defaultLocat.stockNum = num - totalStockNum!!
        }
        this.locatList = locationList
    }

    fun getLocText(storehouseLocatId: Int, storehouseLocatName: String): String {
        if (locatList == null || locatList!!.isEmpty()) {
            return storehouseLocatName
        }
        val text = StringBuilder()
        totalStockNum = 0
        if (SortNumType.Type.NUM_TYPE_STOCK == defaultNumType) {
            for (locatListBean in locatList!!) {
                if (locatListBean == null) {
                    continue
                }
                if (locatListBean.stockNumInt == 0) {
                    continue
                }
                if (storehouseLocatId != locatListBean.storehouseLocatId) {
                    totalStockNum = totalStockNum!! + locatListBean.stockNum
                }
                text.append(locatListBean.storehouseLocatName)
                    .append("(")
                    .append(locatListBean.stockNum)
                    .append(") ")
            }
        } else {
            for (locatListBean in locatList!!) {
                if (locatListBean == null) {
                    continue
                }
                if (storehouseLocatId != locatListBean.storehouseLocatId) {
                    totalStockNum = totalStockNum!! + locatListBean.stockNumInt
                }
                if (locatListBean.storageNum == 0) {
                    continue
                }
                text.append(locatListBean.storehouseLocatName)
                    .append("(")
                    .append(locatListBean.storageNum)
                    .append(") ")
            }
        }
        return text.toString()
    }

    fun getLocatProductText(storehouseLocatName: String): String {
        val text = java.lang.StringBuilder()
        if (locatList == null || locatList!!.isEmpty()) {
            return storehouseLocatName
        }
        for (locatListBean in locatList!!) {
            if (locatListBean == null) {
                continue
            }
            val productList = locatListBean.productList
            if (productList == null || productList.isEmpty()) {
                continue
            }
            text.append(locatListBean.storehouseLocatName)
                .append("(")
                .append(productList.size)
                .append(") ")
        }
        return text.toString()
    }

    fun getLocTextByProducts(storehouseLocatName: String): String {
        if (locatList == null || locatList!!.isEmpty()) {
            return storehouseLocatName
        }
        val text = StringBuilder()
        totalStockNum = 0
        for (locatListBean in locatList!!) {
            if (locatListBean == null) {
                continue
            }
            val productList = locatListBean.productList
            if (productList == null || productList.isEmpty()) {
                continue
            }
            val locatName = locatListBean.storehouseLocatName
            if (TextUtils.isEmpty(locatName)) {
                continue
            }
            text.append(locatName)
                .append("(")
                .append(productList.size)
                .append(") ")
        }
        return text.toString()
    }

    fun removeDefaultLocat(storehouseLocatId: Int) {

        var defaultLocat: LocatListBean? = null
        if (locatList != null) {
            //是否包含临时仓位
            for (listBean in locatList!!) {
                if (listBean == null) {
                    continue
                }
                if (listBean.storehouseLocatId == storehouseLocatId) {
                    defaultLocat = listBean
                    break
                }
            }
            if (defaultLocat != null) {
                locatList!!.remove(defaultLocat)
            }
        }
    }

    fun isLocatListProducts(productBean: ProductBean?): Boolean {
        if (locatList == null || locatList!!.isEmpty()) {
            return false
        }
        for (locatListBean in locatList!!) {
            val productList = locatListBean?.productList
            if (productList == null || productList.isEmpty()) {
                continue
            }
            for (bean in productList) {
                if (bean.productId === productBean?.productId) {
                    return true
                }
            }
        }
        return false
    }

    fun getGoodNum(storehouseLocationId: Int): Int {
        if (this.locatList == null) {
            return 0
        }
        var totalCount = 0
        for (locatListBean in this.locatList!!) {
            val storehouseLocatId = locatListBean!!.storehouseLocatId
            val stockNumInt = locatListBean.stockNumInt
            totalCount += stockNumInt
            if (storehouseLocatId == storehouseLocationId) {
                this.defaultStockNum = this.stockNum()
            }
        }
        return totalCount
    }

    fun getProductCount(): Int {
        //单品
        if (locatList == null) {
            return 0
        }
        var count = 0
        for (locatListBean in locatList!!) {
            val productList = locatListBean?.productList
            if (productList != null) {
                count += productList.size
            }
        }
        return count
    }

    /**
     * 待归还数量
     */
    fun getRemandNum(): Int {
        if (lendNum == null) {
            return 0
        }
        if (returnNum == null) {
            return lendNum!!
        }
        return if (excNum == null) {
            lendNum!! - returnNum!!
        } else {
            lendNum!! - returnNum!! - excNum!!
        }
    }

    /**
     * 待处理数量
     */
    fun getHandleNumInt(): Int {
        //申请数量-已处理数量-已领用数量
        if (applyNum == null) {
            return 0
        }
        if (lendNum == null) {
            return applyNum as Int
        }
        if (changeReceiveNum == null) {
            return applyNum as Int - lendNum as Int
        }

        return applyNum as Int - lendNum as Int - (changeReceiveNum as Int)
    }

    fun totalStockNum(): Int {
        if (locatList == null || locatList!!.isEmpty()) {
            return 0
        }
        var total = 0
        for (listBean in locatList!!) {
            if (listBean == null) {
                continue
            }
            val stockNum = listBean.stockNum
            total += stockNum ?: 0
        }
        return total
    }

    fun totalStorageNumNum(): Int {
        if (locatList == null || locatList!!.isEmpty()) {
            return 0
        }
        var total = 0
        for (listBean in locatList!!) {
            if (listBean == null) {
                continue
            }
            val stockNum = listBean.storageNum
            total += stockNum ?: 0
        }
        return total
    }

    fun getReceiveCount(): Int {
        return if (1 == manageMode && isPasteCode == true) {
            //单品
            val productList: List<ProductBean> = productList ?: return 0
            productList.size
        } else {
            if (locatList == null) {
                return 0
            }
            var count = 0

            for (locatListBean in locatList!!) {
                locatListBean?.stockNum?.let {
                    count += it
                }
            }
            count
        }
    }

    fun changeSort(): Boolean {
        if (isChangeSort == null) {
            return false
        }
        return isChangeSort!!
    }

    fun receiveNum(): Int {
        if (receiveNum == null) {
            return 0
        }
        return receiveNum!!
    }

    fun getApplyTotalNum(sortBeans: List<SortBean>?): Int {
        if (sortBeans == null || sortBeans.isEmpty()) return 0
        var totalCount = 0
        for (sortBean in sortBeans) {
            if (sortId == sortBean.sortId) {
                continue
            }
            totalCount += if (sortBean.applyNum == null) {
                0
            } else {
                sortBean.applyNum!!
            }
        }
        return totalCount
    }
}

fun createProductSortBean(productBean: ProductBean): SortBean {
    val sortBean = SortBean()
    sortBean.itemViewType = SortBeanType.Type.VIEW_TYPE_PRODUCT
    sortBean.productBean = productBean
    sortBean.selected = true
    return sortBean
}

interface SortNumType {
    object Type {
        const val NUM_TYPE_STOCK = 4
        const val NUM_TYPE_STORAGE = 5
    }
}

interface SortBeanType {
    object Type {
        const val VIEW_TYPE_SORT = 0
        const val VIEW_TYPE_PRODUCT = 1
    }

    object ItemViewType {
        const val VIEW_TYPE_ITEM = 1
        const val VIEW_TYPE_HEADER = 2
        const val VIEW_TYPE_SUB_HEADER = 3
    }
}

class AddSortBean(
    var leftTitle: String? = null,
    var isEditable: Boolean = false,
    var rightTitle: String? = null,
    var hint: String? = null,
    var isSelectable: Boolean = false
) : Serializable